import re

from exts import mysql
from util.detailfunction import redefine


def reservation(rid,num):#订阅酒店
    cursor = mysql.cursor()
    cursor.execute("SELECT choice_num FROM`rooms` WHERE roomid="+str(rid))
    t = cursor.fetchall()
    print(t[0],t[0][0])
    hotelroom=t[0][0]
    hotelroom-=int(num)
    sql = "update rooms set choice_num="+str(hotelroom)+" where roomid="+str(rid)

    try:
       cursor.execute(sql)
       # print("无误")
       mysql.commit()
    except:
        # 如果发生错误则回滚
        mysql.rollback()
        print("更新失败")
    # 关闭光标对象
    cursor.close()


def td(rid, num):  # 订阅酒店
    cursor = mysql.cursor()
    cursor.execute("SELECT choice_num FROM`rooms` WHERE roomid=" + str(rid))
    t = cursor.fetchall()
    hotelroom = t[0][0]
    hotelroom += num
    sql = "update rooms set choice_num=" + str(hotelroom) + " where roomid=" + str(rid)
    try:
        cursor.execute(sql)
        # print("无误")
        mysql.commit()
    except:
        # 如果发生错误则回滚
        mysql.rollback()
        print("更新失败")
    # 关闭光标对象
    cursor.close()

def add(email,hid,rid,checkin,checkout,num,day):
    cursor = mysql.cursor()
    cursor.execute("SELECT COUNT(*) FROM `roomorder` ")
    t=cursor.fetchall()
    temp=t[0][0]
    oid=-1
    if temp==None:
        oid==0
    else:
        cursor.execute("SELECT MAX(oid) FROM `roomorder` ")
        t=cursor.fetchone()
        oid=t[0]+1
    sql = "INSERT INTO roomorder (oid,email, hid, rid,checkin,checkout,num,day) VALUES (%d,'%s', %d,%d,'%s','%s',%d,%d)" % (oid,email, hid, rid,checkin,checkout,int(num),int(day))
    print(sql)
    try:
        # 执行sql语句
        cursor.execute(sql)
        # 执行sql语句
        mysql.commit()
    except:
        # 发生错误时回滚
        mysql.rollback()


def delete(oid):
    cursor = mysql.cursor()  # 执行完毕返回的结果集默认以元组显示
    # 定义要执行的SQL语句
    sql = "delete from roomorder where oid="+oid
    try:
        cursor.execute(sql)
        mysql.commit()
    except:
        # 如果发生错误则回滚
        mysql.rollback()
        print("更新失败")
    # 关闭光标对象
    cursor.close()

def displayorderroom(rid):#房间
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM`rooms` WHERE roomid="+rid)
    t = cursor.fetchall()
    result = []
    for i in t:
        temp_string = i[9]
        numbers = [int(temp) for temp in temp_string.split() if temp.isdigit()]
        price = i[10]
        number = re.findall("\d+", str(price))
        lens = len(number)
        jie = 0
        numberprice = 0
        for x in range(lens - 1, -1, -1):
            # print(int(number[x]))
            numberprice += int(number[x]) * (1000 ** jie)
            jie += 1
        resultdict = dict(hid=i[0],hotelname=i[2],roomname=i[3], bedtype=i[5], maxpeople=numbers[0],price=numberprice,roomfacility=redefine(i[7]),roomfacilities=redefine(i[8]),roompriceother=redefine(i[11]),roomsbreakfast=redefine(i[12]),roomcancel=redefine(i[13])[0],pay=redefine(i[14]),detail=redefine(i[15]),roomid=i[16])
        print(resultdict)
        result.append(resultdict)
    #print(result)
    return result

def orderdisplay(email):
    cursor = mysql.cursor()
    sql="SELECT * FROM `roomorder` WHERE email=" +"'"+email+"'"
    print(sql)
    cursor.execute(sql)
    t = cursor.fetchall()
    result=[]
    for i in t:
        result.append(i)
    return result

def checkoid(oid):
    cursor = mysql.cursor()
    cursor.execute("SELECT * FROM`roomorder` WHERE oid=" + oid)
    t = cursor.fetchall()
    result=[]
    for i in t:
        result.append(i)
    return result

if __name__ == '__main__':

    #reservation('1',1)
    #td('1',1)
   add('213201199@seu.com',1,2,'ceshi1','ceshi2',1,1)
   add('213201199@seu.com',1,2,'ceshi1','ceshi2',2,1)
   add('213201199@seu.com', 1, 2, 'ceshi1', 'ceshi2',3,2)
   add('213201199@seu.com', 1, 2, 'ceshi1', 'ceshi2',4,3)
   add('213201199@seu.com', 1, 2, 'ceshi1', 'ceshi2',5,4)
   add('213201199@seu.com', 1, 2, 'ceshi1', 'ceshi2',6,2)
   add('213201199@seu.com', 1, 2, 'ceshi1', 'ceshi2',7,1)
   add('213201199@seu.com', 1, 2, 'ceshi1', 'ceshi2',8,1)
   #delete('7')